﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUL;
using Public;
using System.Globalization;

namespace Presentation
{
    public partial class frmManageStaff : Form
    {
        ManageStaffBUL staffBUL = new ManageStaffBUL();
        public frmManageStaff()
        {
            InitializeComponent();
            LoadStaff();
            LoadRole();
            dgvStaff.ClearSelection();
        }

        public void LoadStaff()
        {
            dgvStaff.DataSource = staffBUL.GetStaffBUL();
        }

        public void LoadRole()
        {
            cbbRole.DataSource = staffBUL.GetRoleBUL().Tables[0];
            cbbRole.ValueMember = "roleId";
            cbbRole.DisplayMember = "roleName";
        }

        private void dgvStaff_MouseClick(object sender, MouseEventArgs e)
        {
            txtUsername.Text = "";
            txtFullname.Text = "";
            txtPassword.Text = "";
            //txtRole.Text = "";
          //  txtStaredDate.Text = "";
            txtPhone.Text = "";
            txtAddress.Text = "";
        //    txtBirthday.Text = "";
            //txtSex.Text = "";
            cbbSex.SelectedIndex = 0;

            txtUsername.Text = dgvStaff.SelectedRows[0].Cells[1].Value.ToString();
            txtPassword.Text = dgvStaff.SelectedRows[0].Cells[2].Value.ToString();
            txtFullname.Text = dgvStaff.SelectedRows[0].Cells[3].Value.ToString();
            //txtRole.Text = dgvStaff.SelectedRows[0].Cells[5].Value.ToString();
            cbbRole.SelectedValue = dgvStaff.SelectedRows[0].Cells[5].Value;
            try
            {
               sDate.Value = DateTime.Parse(dgvStaff.SelectedRows[0].Cells[9].Value.ToString()).Date;
            }
            catch (Exception)
            {
                
            }

            txtPhone.Text = dgvStaff.SelectedRows[0].Cells[7].Value.ToString();
            txtAddress.Text = dgvStaff.SelectedRows[0].Cells[8].Value.ToString();
            try
            {
                
            dtBirthday.Value = DateTime.Parse(dgvStaff.SelectedRows[0].Cells[6].Value.ToString()).Date;
            }
            catch (Exception)
            {
                
            }
        /*    try
            {
                txtBirthday.Text = DateTime.Parse(dgvStaff.SelectedRows[0].Cells[6].Value.ToString()).Date.ToString("d");
            }
            catch (Exception)
            {
                //txtBirthday.Text = "";
            } */

            //txtSex.Text = dgvStaff.SelectedRows[0].Cells[4].Value.ToString();
            string sex = dgvStaff.SelectedRows[0].Cells[4].Value.ToString();
            if (sex == "True")
            {
                cbbSex.SelectedIndex = 0;
            }
            else
            {
                cbbSex.SelectedIndex = 1;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddStaff();
            LoadStaff();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateStaff();
            LoadStaff();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            DeactiveStaff();
            LoadStaff();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Hide();
        }

        public void AddStaff()
        {
            if (!staffBUL.CheckExistedStaffDAL(txtUsername.Text))
            {
                if (txtUsername.Text != "" && txtPassword.Text != "" && txtFullname.Text != "")
                {


                    try
                    {
                        string username = txtUsername.Text;
                        string password = txtPassword.Text;
                        string name = txtFullname.Text;
                        int male = 0;
                        if (cbbSex.SelectedIndex == 1)
                        {
                            male = 1;
                        }
                        else if (cbbSex.SelectedIndex == 2)
                        {
                            male = 0;
                        }
                        int roleId = Int32.Parse(cbbRole.SelectedValue.ToString());
                        string birthday = dtBirthday.Value.ToString("yyyy/MM/dd");
                        string phone = txtPhone.Text;
                        string address = txtAddress.Text;
                        string startedDate = sDate.Value.ToString("yyyy/MM/dd");
                        int isShow = 1;

                        staffBUL.AddStaffBUL(username, password, name, male, roleId, birthday, phone, address, startedDate, isShow);
                        ClearFields();

                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Some error occurs, please input correct information.");
                    }
                }
                else MessageBox.Show("Some error occurs, please input correct information.");

            }
            else
            {
                MessageBox.Show("This username already existed.");
            }

        }

        public void UpdateStaff()
        {
            //if (!staffBUL.CheckExistedStaffDAL(txtUsername.Text))
            //{
            if (txtUsername.Text != "" && txtPassword.Text != "" && txtFullname.Text != "")
            {
                try
                {
                    string userId = dgvStaff.SelectedRows[0].Cells[0].Value.ToString();
                    string username = txtUsername.Text;
                    string password = txtPassword.Text;
                    string name = txtFullname.Text;
                    int male = 0;
                    if (cbbSex.SelectedIndex == 1)
                    {
                        male = 1;
                    }
                    else if (cbbSex.SelectedIndex == 2)
                    {
                        male = 0;
                    }
                    //string s = DateTime.Parse(txtBirthday.Text).Date.ToString("yyyy/MM/dd");
                    int roleId = Int32.Parse(cbbRole.SelectedValue.ToString());
                    string birthday = dtBirthday.Value.ToString("yyyy/MM/dd");
                    string phone = txtPhone.Text;
                    string address = txtAddress.Text;
                    string startedDate = sDate.Value.ToString("yyyy/MM/dd");
                    int isShow = 1;

                    staffBUL.UpdateStaffBUS(userId, username, password, name, male, roleId, birthday, phone, address, startedDate, isShow);
                    userId = "";
                    MessageBox.Show("Updated!!!");
                    ClearFields();
                }
                catch (Exception)
                {
                    MessageBox.Show("Some error occurs, please input correct information.");
                }
            }
            else MessageBox.Show("Some error occurs, please input correct information.");

            //}
            //else
            //{
            //    MessageBox.Show("This username already existed.");
            //}

        }

        public void AddUser()
        {
            User user = new User();
            List<User> lstUser = new List<User>();
            user.Username = txtUsername.Text;
            user.Password = txtPassword.Text;
            user.Name = txtFullname.Text;
            //user.RoleId = Int32.Parse(txtRole.Text);
            user.StartedDate = sDate.Value.Date;
            user.Phone = txtPhone.Text;
            user.Address = txtAddress.Text;
            user.Birthday = dtBirthday.Value.Date;
            if (cbbSex.SelectedIndex == 1)
            {
                user.Sex = true;
            }
            else if (cbbSex.SelectedIndex == 2)
            {
                user.Sex = false;
            }
            else
            {
                MessageBox.Show("Please select gender.");
            }

            lstUser.Add(user);

            var q = lstUser.ToList();
        }


        private void ClearFields()
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtFullname.Text = "";
            //txtRole.Text = "";
            //txtStaredDate.Text = "";
            txtPhone.Text = "";
            txtAddress.Text = "";
            //txtBirthday.Text = "";
            cbbSex.SelectedIndex = 0;
            dtBirthday.Text = DateTime.Now.ToString();
            sDate.Text = DateTime.Now.ToString();
        }

        public void DeactiveStaff()
        {
            if (MessageBox.Show("Are you sure to delete this user?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string userId = dgvStaff.SelectedRows[0].Cells[0].Value.ToString();
                staffBUL.DeactiveStaffBUS(userId);
            }
        }

        private void dgvStaff_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewColumn collumn = dgvStaff.Columns[e.ColumnIndex];
            try
            {
                if (collumn.Name == "clSex")
                {
                    bool sex = Convert.ToBoolean(e.Value);
                    e.Value = sex ? "Male" : "Female";
                }
            }
            catch (Exception)
            {
                e.Value = "";
            }
        }

        private void frmManageStaff_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Hide();
        }

    }
}
